package com.mida.app.report.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.service.DocStructTemplateAuthSpaceService;
import com.mida.bms.report.api.DocStructTemplateAuthSpaceApi;
import com.mida.bms.report.entity.DocStructTemplateAuthSpace;
import com.mida.bms.report.vo.DocStructTemplateAuthSpaceVo;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.vo.provider.BaseOrgInfoVo;
import com.mida.dms.mdm.vo.provider.BusinessUnitVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * @ClassName: DocStructTemplateAuthSpaceServiceImpl
 * @Description: 结构化模板授权空间impl
 * @Author: wang xiao le
 * @Date: 2021/6/18 20:03
 **/
@Service
@Slf4j
@Transactional
public class DocStructTemplateAuthSpaceServiceImpl implements DocStructTemplateAuthSpaceService {

    @Autowired
    private DocStructTemplateAuthSpaceApi docStructTemplateAuthSpaceApi;

    @Autowired
    private MdmServiceImpl mdmService;

    @Override
    public ListResult<DocStructTemplateAuthSpaceVo> get(long stTempAuthSpSoid) {
        log.info("开始获取授权空间:" + stTempAuthSpSoid);
        StopWatch stopWatch = StopWatch.createStarted();
        ListResult<DocStructTemplateAuthSpaceVo> lr = new ListResult<>();

        try {
            ListResult<DocStructTemplateAuthSpaceVo> docStructTemplateAuthSpaceVoListResult =
                    docStructTemplateAuthSpaceApi.selectByAuthSpSoid(stTempAuthSpSoid);
            DocStructTemplateAuthSpaceVo docStructTemplateAuthSpaceVo =
                    docStructTemplateAuthSpaceVoListResult.getData();

            // 设置回显相关数据
            setDocStructTemplateAuthSpaceVo(docStructTemplateAuthSpaceVo);

            buildListSuccess(lr, docStructTemplateAuthSpaceVo);
        } catch (Exception e) {
            log.error("获取单个授权空间发送异常", e);
            buildListWarn(lr, "获取授权空间失败");
        } finally {
            stopWatch.stop();
            log.info("本次查询耗时:" + stopWatch.getTime() + "ms");
        }
        return lr;
    }

    /**
     * 设置回显相关数据
     * @param docStructTemplateAuthSpaceVo
     */
    private void setDocStructTemplateAuthSpaceVo(DocStructTemplateAuthSpaceVo
                                                         docStructTemplateAuthSpaceVo) {
        //回显医院名称
        if (docStructTemplateAuthSpaceVo.getHospitalSoid() != null) {
            ListResult<BaseOrgInfoVo> orgByOrgInfoResult =
                    mdmService.findOrgByOrgInfoSoid(
                            docStructTemplateAuthSpaceVo.getHospitalSoid(),
                            false);
            if (orgByOrgInfoResult.getData() != null) {
                docStructTemplateAuthSpaceVo.setHospitalName(
                        orgByOrgInfoResult.getData().getOrgInfoName());
            }
        }
        //回显业务单元名称
        docStructTemplateAuthSpaceVo.getDocUseSpaceUnitVoList().forEach(vo -> {
            if (StringUtils.isNotEmpty(vo.getBusinessUnitSoid().toString())) {
                ListResult<BusinessUnitVo> businessUnit =
                        mdmService.findBusinessUnitBySoid(
                                vo.getBusinessUnitSoid(),
                                false);
                if (businessUnit.getData() != null) {
                    vo.setBusinessUnitName(businessUnit.getData().getBusinessUnitName());
                }
            }
        });
        //回显授权人员名称
        docStructTemplateAuthSpaceVo.getDocUseSpaceConsumerVoList().forEach(vo -> {
            if (StringUtils.isNotEmpty(vo.getDoctorSoid().toString())) {
                ListResult<EmployeeInfoVo> employeeInfoVoListResult =
                        mdmService.findOneBySoid(vo.getDoctorSoid(),
                                false);
                if (employeeInfoVoListResult.getData() != null) {
                    vo.setEmployeeName(
                            employeeInfoVoListResult.getData().getEmployeeName());
                }
            }
        });
    }

    /**
      * @Author WXL
      * @Description  结构化模板报告授权空间下拉(查询所有授权空间)
      * @Date  2021/7/27
      * @Param [searChData]
      * @return ListResult<List<DocStructTemplateAuthSpace>>
      **/
    @Override
    public ListResult<List<DocStructTemplateAuthSpace>> findAll(String searChData) {
        ListResult<UserDto> user = mdmService.getLoginUserInfo();
        Long hospitalSoid = null;
        if (ObjectUtils.isNotEmpty(user.getData())) {
            hospitalSoid = user.getData().getHospitalSoid();
        }
        return docStructTemplateAuthSpaceApi.findAll(searChData, hospitalSoid);
    }
}
